/**
 * Spring 2012 TCSS360 Deliverable 3
 * Otis Alexander
 */

package storage;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class WriteXMLFile {
	private DocumentBuilderFactory dBF;
	private DocumentBuilder dB;
	private Document doc;

	public WriteXMLFile() {
		dBF = DocumentBuilderFactory.newInstance();
		try {
			dB = dBF.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		doc = dB.newDocument();
	}

	public Document getDoc() {
		return doc;
	}

	public void setDoc(Document d) {
		doc = d;
	}

	public void writeFile() {
		TransformerFactory tf =
				TransformerFactory.newInstance();
		try {
			Transformer t = tf.newTransformer();
			t.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource s = new DOMSource(doc);
			StreamResult result =
					new StreamResult(new File("file.xml"));
			t.transform(s, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
}
